package com.wnxy.sdd.front.controller;

import com.wnxy.sdd.front.entity.CourseDescription;
import com.wnxy.sdd.front.entity.Subject;
import com.wnxy.sdd.front.service.ICourseService;
import com.wnxy.sdd.front.service.ISubjectService;
import com.wnxy.sdd.front.service.IVideoService;
import com.wnxy.sdd.front.vo.CourseVo;
import com.wnxy.sdd.front.vo.IndexCourseQueryVo;
import com.wnxy.sdd.front.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ISubjectService subjectService;
    @Autowired
    public ICourseService courseService;
    @Autowired
    private IVideoService videoService;

    @GetMapping
    public String queryAll(
            @RequestParam(required = false, defaultValue = "1") String currentPage,
            @RequestParam(required = false, defaultValue = "4") String pageSize,
            @RequestParam(required = false) String subjectId,
            Model model){
        // 1. 查询所有二级分类科目
        List<Subject> subjectList =  subjectService.querySubjectTwos();
        // 2. 分页查询课程列表数据
        List<IndexCourseQueryVo> courseList =
                courseService.queryPage(
                        Integer.parseInt(currentPage), Integer.parseInt(pageSize),subjectId);
        // 3. 将数据绑定到数据模型
        model.addAttribute("subjectList", subjectList);
        model.addAttribute("courseList", courseList);
        model.addAttribute("subjectId", subjectId);
        // 4. 返回视图名称
        return "course_list";
    }

    @RequestMapping("/{id}")
    public String courseDetail(@PathVariable("id")String id,Model model) {
        // 1. 数据查询
        // 1.1 根据课程id查询
        CourseVo courseVo = courseService.queryCourseVoById(id);
        // 1.2 查询课程下的视频
        List<VideoVo> videoVoList = videoService.queryVideoByCourseId(id);
        // 1.3 精选好课
        List<CourseVo> goodCourseList = courseService.findCourseByBuyCount(4);
        // 2. 保存
        model.addAttribute("courseVo",courseVo);
        model.addAttribute("videoVoList",videoVoList);
        model.addAttribute("goodCourseList",goodCourseList);
        // 2. 课程详情页面
        return "course_detail";
    }

}